﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xejen.Languages
{
    /// <inheritdoc cref="ILanguage"/>
    internal class Language : ILanguage
    {
        private readonly LanguageJsonData _currentLanguageJsonData;

        /// <inheritdoc/>
        public string LanguageName { get; }

        /// <inheritdoc/>
        public string LanguageCode { get; }

        /// <inheritdoc cref="Language"/>
        internal Language(LanguageJsonData data)
        {
            _currentLanguageJsonData = data;
            this.LanguageName = _currentLanguageJsonData.LanguageName;
            this.LanguageCode = _currentLanguageJsonData.LanguageCode;
        }

        /// <inheritdoc/>
        public string this[string key]
        {
            get
            {
                string[] keys = key.Split('.');
                JToken currentToken = JObject.FromObject(_currentLanguageJsonData.Translations);

                foreach (var k in keys)
                {
                    if (currentToken is JObject currentObject && currentObject.TryGetValue(k.Trim(), out JToken nextToken))
                    {
                        currentToken = nextToken;
                    }
                    else
                    {
                        //throw new KeyNotFoundException($"Key '{key}' not found in translations.");
                        return string.Empty;
                    }
                }

                return currentToken.ToString();
            }
        }
    }
}
